# ---- Builder Stage ----
# Use the official Go image as a builder.
# Specify the Go version to ensure consistency.
FROM golang:1.23-alpine AS builder

# Set the working directory inside the container.
WORKDIR /app

# 设置 GOPROXY 以加速依赖下载并解决网络连接问题
ENV GOPROXY=https://goproxy.cn,direct
ENV GOSUMDB=off

# Copy go.mod and go.sum files to download dependencies first.
# This leverages Docker layer caching.
COPY go.mod go.sum ./
RUN go mod download

# Copy the entire source code.
COPY . .

# Build the Go application.
# CGO_ENABLED=0 is important for creating a static binary that can run in a minimal container.
# -ldflags="-w -s" strips debugging information, reducing the binary size.
RUN CGO_ENABLED=0 GOOS=linux go build -a -ldflags="-w -s" -o /mcp-link-engine ./main.go

# ---- Final Stage ----
# Use a minimal, empty base image.
FROM scratch

# Copy only the compiled application from the builder stage.
COPY --from=builder /mcp-link-engine /mcp-link-engine

# Expose the port the application will run on.
# From our analysis of main.go, we know the default port is 8080.
EXPOSE 8080

# Set the entrypoint for the container.
# The application will start when the container launches.
ENTRYPOINT ["/mcp-link-engine", "serve", "--host", "0.0.0.0", "--port", "8080"]